﻿using System;
using System.Web;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.BLL;
namespace WinnerCMS.HttpModule
{
    public class UrlRewriter : IHttpModule
    {
        private static readonly string[] PathExtensions = { ".htm", string.Empty };
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此模块，
        /// 并向 IIS 注册此模块，然后才能使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members
        public void Dispose()
        {
            //此处放置清除代码。
        }

        public void Init(HttpApplication context)
        {
            context.AuthorizeRequest += new EventHandler(this.AuthorizeRequest_URLRewriter);
        }
        #endregion

        protected void AuthorizeRequest_URLRewriter(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;

            if (PathExtensions.Any(x => context.Request.CurrentExecutionFilePathExtension.Equals(x)) == false)
            {
                return;
            }

            string rawUrl = context.Request.RawUrl;
            Rules regexList = BLL.CacheHepler.RewriterCache.ReadRewriterRule(context);
            foreach (BLL.Rule r in regexList.Datas)
            {
                Match m = r.NewPathRegex.Match(rawUrl);//匹配
                if (m.Success)//成功
                {
                    //匹配出其真实的URL
                    rawUrl = r.NewPathRegex.Replace(rawUrl, regexList.Templates + r.RealPath);
                    RewriteUrl(context, rawUrl);

                    return;
                }
            }
        }

        internal static void RewriteUrl(HttpContext context, string sendToUrl)
        {
            string sendToUrlLessQString;
            string filePath;
            RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath);
        }
        internal static void RewriteUrl(HttpContext context, string sendToUrl, out string sendToUrlLessQString, out string filePath)
        {
            if (context.Request.QueryString.Count > 0)
            {
                if (sendToUrl.IndexOf('?') != -1)
                {
                    sendToUrl = sendToUrl + "&" + context.Request.QueryString;
                }
                else
                {
                    sendToUrl = sendToUrl + "?" + context.Request.QueryString;
                }
            }
            string queryString = string.Empty;
            sendToUrlLessQString = sendToUrl;
            if (sendToUrl.IndexOf('?') > 0)
            {
                sendToUrlLessQString = sendToUrl.Substring(0, sendToUrl.IndexOf('?'));
                queryString = sendToUrl.Substring(sendToUrl.IndexOf('?') + 1);
            }

            filePath = context.Server.MapPath(sendToUrlLessQString);
            context.RewritePath(sendToUrlLessQString, string.Empty, queryString);
        }

    }
}
